﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;

namespace DotNetNuke.Tests.UI.WatiN.UpgradeTests
{
    [TestFixture]
    public class UpgradeTests : UpgradeBase
    {
        #region Constructors
       

        #endregion

        #region Test Setup and TearDown
        [SetUp]
        public void SetUp()
        {
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }
        #endregion


        #region Tests
        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        public void UpgradeTest(string version, string type)
        {
            //Open a new Internet Explorer window and navigate to the website
            ieInstance = WatiNUtil.OpenIEInstance(SiteURL, SilentMode, IETimeOut, AutoCloseIE, AutoMovePointer);
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            if (!Directory.Exists(CapturePath))
            {
                Directory.CreateDirectory(CapturePath);
            }

            //Wait until install completes
            if (!ieInstance.Text.Contains("Upgrading DotNetNuke"))
            {
                if (File.Exists(CapturePath + @"\UpgradeNotStarted.jpg"))
                {
                    File.Delete(CapturePath + @"\UpgradeNotStarted.jpg");
                }
                ieInstance.CaptureWebPageToFile(CapturePath + @"\UpgradeNotStarted.jpg");

            }
            Assert.IsTrue(ieInstance.Text.Contains("Upgrading DotNetNuke"), @"The upgade was not started. For more information on the error view the screenshot for this test under Tests\Screen Shots.");
            ieInstance.WaitUntilContainsText("Click Here To Access Your Portal", IETimeOut);
            if (File.Exists(CapturePath + @"\Upgrade.jpg"))
            {
                File.Delete(CapturePath + @"\Upgrade.jpg");
            }
            ieInstance.CaptureWebPageToFile(CapturePath + @"\Upgrade.jpg");
            if (ieInstance.Text.Contains("Error") || ieInstance.Text.Contains("Failure"))
            {
                TestLog.WriteHighlighted("WARNING:");
                TestLog.WriteLine(@" An Error has occurred during the upgrade however the tests may continue. For more information on the error view the screenshot for this test under Tests\Screen Captures.");
            }

            //Click on link to go to Home Page
            ieInstance.Link(Find.ByText("Click Here To Access Your Portal")).Click();

            //Assert
            Assert.IsTrue(homePage.LogoLink.Exists);
            Assert.IsTrue(homePage.LoginLink.Exists);
            Assert.IsTrue(homePage.RegisterLink.Exists);

            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
            HostSQLPage SQLPage = new HostSQLPage(homePage);
            DateTime currentDate = DateTime.Now;
            int year = currentDate.Year;
            int month = currentDate.Month;
            int day = currentDate.Day;
            SQLPage.SQLQueryTextField.Value = "SELECT LogTypeKey, LogCreateDate, LogProperties FROM EventLog WHERE LogTypeKey LIKE '%EXCEPTION' AND LogCreateDate>'" + year + "-" + month +"-" + (day-1) +" 23:59:59.996'";
            SQLPage.ExecuteSQLLink.Click();
            System.Threading.Thread.Sleep(2000);
            if (!SQLPage.QueryResults.InnerHtml.Contains("The query did not return any data"))
            {
                TableRowCollection resultsRows = SQLPage.QueryResults.TableRows.Filter(Find.ByClass("Normal"));
                TestLog.WriteHighlighted("WARNING: ");
                TestLog.WriteLine("One or more exceptions occured during the upgrade however the tests may continue. For more information view the test information from this test in the Screen Captures folder. \n");
                if (File.Exists(CapturePath + @"\UpgradeException.txt"))
                {
                    File.Delete(CapturePath + @"\UpgradeException.txt");
                }
                StreamWriter sWriter = File.CreateText(CapturePath + @"\UpgradeException.txt");
                foreach (TableRow row in resultsRows)
                {
                    sWriter.WriteLine(row.OwnTableCells[0].Text + " on " + row.OwnTableCells[1] + ":");
                    sWriter.WriteLine(row.OwnTableCells[2].Text);
                }
                sWriter.Close();
            }
        }

        #endregion

        #region Methods

        #endregion



    }
}
